<?php

$conn = mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db("mydb", $conn) or die(mysql_error());

if (isset($_GET['submit']))
{
    $conf_id = $_GET['conference_id'];
    $value = $_GET['rating_value']; // i save the rating value the user enters in the text box in a value to be able to enter it in the database
    $id = $_GET['rate']; // this value will get the ID of the rating criteria the user chosen in the drop down list


    $query = mysql_query
            ("SELECT Rate_id,Conference_id FROM conference_rating_criteria WHERE conference_rating_criteria.Rate_id ='$id'
             AND conference_rating_criteria.Conference_id ='" . $conf_id . "'") or die('here' . mysql_error());
    // this query selects the rate criteria that are assigned to this conference by comparing the id the 
    // got from the drop down list where the conference id = conference id. which will reterive
    // the rate id of all the criterira assigened to this conference.
    $row = mysql_fetch_assoc($query);
    $row['Rate_id'];
    $sp_id = $_GET['span'];
    $ver_id = $_GET['version_id'];
    $result = mysql_query("INSERT into version_rating_criteria 
            (version_id,rate_id,span_id,rating_value)
           VALUES('$ver_id','$id','$sp_id','$value')") or die(mysql_error());
    // this query will insert the rating value that the user has entered with the rate_id i got from the above query
    // and the version id i got from the session.

// here i'll calculate the average to get the final rate by adding all the rating values divided by the sum of all rate ids
$sql_query = mysql_query("select COUNT(rate_id) FROM version_rating_criteria WHERE version_id='{$ver_id}'") // sum of all rate_id
        or die(mysql_error());
$row = mysql_fetch_assoc($sql_query);
$x = $row['COUNT(rate_id)'];


$sql_query2 = mysql_query("SELECT SUM(rating_value) FROM version_rating_criteria WHERE version_id='{$ver_id}'") // sum of all rating values.
        or die(mysql_error());
$row2 = mysql_fetch_assoc($sql_query2);

$y = $row2['SUM(rating_value)'];

$avgRate = $y / $x;

$paper_id = mysql_query("SELECT paper_id FROM version WHERE version_id = '" . $ver_id . "'")
        or die('here' . mysql_error());
// in this query i want the paper id. so i'll compare the version id by the version id
// i got from the session.

$row = mysql_fetch_assoc($paper_id);
mysql_query("UPDATE paper SET final_rating = '$avgRate' WHERE paper_id = '{$row['paper_id']}'") or die('here1' . mysql_error());

// after i'll get the paper id from the above query. i'll start to insert the final rating in the table member
// where the paper id = the one i have.

mysql_close($conn);
echo "Rating completed successfully";
}
?>
